package com.egg.ylt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_SelectServiceList;
import com.egg.ylt.pojo.GetShopServerListEntity;
import com.egg.ylt.presenter.impl.SelectServePresenterImpl;
import com.egg.ylt.view.ISelectServeItemView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_SelectServiceItem extends BaseActivity<SelectServePresenterImpl> implements ISelectServeItemView {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_tv_title)
    TextView mTitleTv;
    @BindView(R.id.select_service_rlv)
    RecyclerView mSelectServiceRlv;
    @BindView(R.id.target_view_ll)
    LinearLayout mTargetViewLl;
    private ADA_SelectServiceList mSelectServiceList;
    private List<GetShopServerListEntity.ListEntity> mEntities;
    private String mShopId;
    private String mStorePhone;

    @Override
    protected void getBundleExtras(Bundle extras) {
        mShopId = extras.getString("shopId");
        mStorePhone = extras.getString("storePhone");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_select_serve_item;
    }

    @Override
    protected View getLoadingTargetView() {
        return mTargetViewLl;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        //动态设置沉浸式状态栏的高度
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        mTitleTv.setText("选择服务项目");
        //初始化recyclerView
        initListView();
    }

    private void initListView() {
        mEntities = new ArrayList<>();
        //设置布局管理器
        mSelectServiceRlv.setLayoutManager(new LinearLayoutManager(mContext));

        //设置适配器
        mSelectServiceList = new ADA_SelectServiceList(mContext);
        mSelectServiceRlv.setAdapter(mSelectServiceList);

        mSelectServiceList.setOnItemClickListener(new ADA_SelectServiceList.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                GetShopServerListEntity.ListEntity entity = mEntities.get(position);
                Intent intent = new Intent();
                intent.putExtra("id", entity.getId());
                intent.putExtra("serviceImageUrl", entity.getImageUrl());
                intent.putExtra("shopId", mShopId);
                intent.putExtra("storePhone", mStorePhone);
                readyGo(ACT_ServiceItem.class, intent);
            }
        });

        //请求数据
        mPresenter.getServeData(Constants.TOKEN, ACT_ShopAppointment.sShopId);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getServeData(GetShopServerListEntity entity) {

        if (entity != null && ListUtil.isListEmpty(entity.getList())) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无服务项目~");
            return;
        }

        toggleShowEmpty(false, -1, "");

        //得到服务列表数据
        mEntities = entity.getList();
        mSelectServiceList.addList(entity.getList());
    }

    @OnClick({R.id.include_iv_back, R.id.btn_sure_select_service})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.btn_sure_select_service://确定选中
                setIntentData();
                break;
        }
    }

    /**
     * 回传给上一界面的数据
     */
    public void setIntentData() {
        int type = 0;
        Intent intent = new Intent();
        List<GetShopServerListEntity.ListEntity> dataList = mSelectServiceList.getDataList();
        for (int i = 0; i < dataList.size(); i++) {
            GetShopServerListEntity.ListEntity entity = dataList.get(i);
            if (entity.isSelected()) {
                type = 1;
                intent.putExtra("serviceId", entity.getId());
                intent.putExtra("serviceName", entity.getName());
                intent.putExtra("servicePrice", entity.getPrice());
                intent.putExtra("serviceIntro", (StringUtil.isEmpty(entity.getFitAge()) ? "" : (
                        entity.getFitAge() + "岁")) + (StringUtil.isEmpty(entity.getServiceTime()) ? "" :
                        (" | " + (entity.getServiceTime() + "分钟"))) + (StringUtil.isEmpty(entity.getServiceType()) ?
                        "" : (" | " + entity.getServiceType())));
                intent.putExtra("serviceImageUrl", entity.getImageUrl());
            }
        }
        if (type == 0) {
            ToastUtil.makeText(mContext, "请先选择服务项目", false);
            return;
        }
        setResult(Constants.SELECT_SERVICE_ITEM, intent);
        finish();
    }
}
